//
//  PageViewController.m
//  BeautifulWardrobe
//
//  Created by mac on 16/7/27.
//  Copyright © 2016年 Yh. All rights reserved.
//

#import "PageViewController.h"

#import "Delegate.h"

#import "PageSecondVC.h"
@interface PageViewController ()<DelegateDelegate,UICollectionViewDelegate,UICollectionViewDataSource>
{
    UILabel *  _label;
    UIButton * _selecteButton;
    Delegate * _myDelegate;
    NSArray * _pageArray;
}
@end
@implementation PageViewController

- (void)viewDidAppear:(BOOL)animated
{
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    _pageArray = [[NSArray alloc]init];
    NSString * urlStr= @"http://api.yuike.com/gmall/api/1.0/home/floor_list.php?mid=457465";
    NSString * scrollUrl = @"http://api.yuike.com/gmall/api/1.0/home/floor_list.php?mid=457465";
    
    _myDelegate = [[Delegate alloc]init];
    _myDelegate.delegate = self;
    [_myDelegate getScrollViewButtonUrl:scrollUrl];
    [_myDelegate getRequestUrl:urlStr];
    
    [self.view addSubview:_myDelegate];
    
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = self.view.frame.size;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    UICollectionView * collection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 108, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) collectionViewLayout:layout];
    collection.delegate = self;
    collection.dataSource = self;
    collection.pagingEnabled = YES;
    [self.view addSubview:collection];
    [collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return section;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor yellowColor];
    
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.

}
- (void)getButtonTitle:(NSArray *)array
{
    UIScrollView * scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, _myDelegate.bounds.size.width-50, _myDelegate.bounds.size.height)];
    scrollView.showsHorizontalScrollIndicator = NO;
    UIButton * button = nil;
    
    _label = [[UILabel alloc]init];
    
    for (int i = 0; i < array.count;  i++)
    {
        PageSecondVC * pvc = [[PageSecondVC alloc]init];
        [self addChildViewController:pvc];
        button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(i * (scrollView.bounds.size.width)/6, 0, scrollView.bounds.size.width/6, 42);
        [button setTitle:array[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [scrollView addSubview:button];
        if (i == 0)
        {
            button.selected = YES;
            _selecteButton = button;
            _label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, button.bounds.size.width, 2)];
            _label.backgroundColor = [UIColor redColor];
            [_label setCenter:CGPointMake(button.center.x, 41)];
            [scrollView addSubview:_label];
        }
    }
    scrollView.contentSize = CGSizeMake(button.bounds.size.width * array.count, 0);
    [_myDelegate addSubview:scrollView];
}
- (void)buttonClick:(UIButton *)button;
{
    [_label setCenter:CGPointMake(button.center.x, 41)];
    
    button.selected = YES;
    _selecteButton.selected = NO;
    _selecteButton = button;
}
- (void)getHeadStr:(NSString *)str
{
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    UIView * view = [[UIView alloc]initWithFrame:self.navigationController.navigationBar.bounds];
    UIImageView * imgBT = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 44)];
    imgBT.image = [UIImage imageNamed:@"image_titlebar_logo"];
    [view addSubview:imgBT];
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:[UIImage imageNamed:@"image_titlebar_search"] forState:UIControlStateNormal];
    [button setTitle:str forState:UIControlStateNormal];
    UIImageView * buttonImage = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 24, 24)];
    buttonImage.image = [UIImage imageNamed:@"image_titlebar_searchsub"];
    [button addSubview:buttonImage];
    button.frame = CGRectMake(110, 0, view.bounds.size.width-170, view.bounds.size.height);
    [view addSubview:button];
    UIButton * button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    button1.frame = CGRectMake(110 + view.bounds.size.width - 160, 10, 24, 24);
    [button1 setBackgroundImage:[UIImage imageNamed:@"image_titlebar_message"] forState:UIControlStateNormal];
    [view addSubview:button1];
    self.navigationItem.titleView = view;
    self.automaticallyAdjustsScrollViewInsets = NO;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
